package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Members;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OrderMapper;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.service.IOrderService;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.common.utils.StringUtils.isEmpty;
import static com.ruoyi.common.utils.StringUtils.isNotEmpty;

/**
 * 订单Service业务层处理
 * 
 * @author 陈斌斌
 * @date 2025-09-29
 */
@Service
@RequiredArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapper,Order> implements IOrderService
{
    private final OrderMapper orderMapper;


    /**
     * 查询订单
     * 
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public Order selectOrderById(String id)
    {
        if(isEmpty(id)){
            throw new RuntimeException("主键标识不能为空");
        }
        return orderMapper.selectOrderById(id);
    }

    /**
     * 查询订单列表
     * 
     * @param order 订单
     * @return 订单
     */
    @Override
    public List<Order> selectOrderList(Order order)
    {

        return orderMapper.selectOrderList(order);

    }

    /**
     * 新增订单
     * 
     * @param order 订单
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertOrder(Order order)
    {

        return StringUtils.isNotNull(order)?this.baseMapper.insert(order):0;
    }

    /**
     * 修改订单
     * 
     * @param order 订单
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateOrder(Order order)
    {
        if(isEmpty(order.getId())){
            throw new RuntimeException("主键标识不能为空");
        }

        return this.baseMapper.updateById(order);
    }


    /**
     * 删除订单信息
     * 
     * @param id 订单主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteOrderById(String id)
    {
        if(isEmpty(id)){
            throw new RuntimeException("主键标识不能为空");
        }
        return this.baseMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateMidById(Order order) {
        return this.baseMapper.updateById(order);
    }

    @Override
    public Order selectByLast() {
        return orderMapper.selectMembersByLast();
    }
}
